//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "BiCGstab.h"
using namespace Cephei::QL::Math::Matrixutilities;
#include <gen/QL/Math/Matrixutilities/BiCGStabResult.h>
#include <gen/QL/Math/Array.h>
using namespace Cephei::QL::Math;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Math::Matrixutilities::CBiCGstab::CBiCGstab (boost::shared_ptr<QuantLib::BiCGstab>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phBiCGstab = NULL;
#endif
	_ppBiCGstab = &childNative;
    
}
Cephei::QL::Math::Matrixutilities::CBiCGstab::CBiCGstab (QuantLib::BiCGstab& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phBiCGstab = NULL;
#endif
	_ppBiCGstab = new boost::shared_ptr<QuantLib::BiCGstab> (&childNative);
    
    _BiCGstabOwner = owner;
    
}

Cephei::QL::Math::Matrixutilities::CBiCGstab::CBiCGstab (CBiCGstab^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phBiCGstab = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppBiCGstab = new boost::shared_ptr<QuantLib::BiCGstab> (copy->GetShared());
        
    }
}
Cephei::QL::Math::Matrixutilities::CBiCGstab::CBiCGstab (PLATFORM::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phBiCGstab = NULL;
#endif
	if (!t->IsSubclassOf(CBiCGstab::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Math::Matrixutilities::CBiCGstab::CBiCGstab (QuantLib::Handle<QuantLib::BiCGstab>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phBiCGstab = &childNative;
	_ppBiCGstab = &static_cast<boost::shared_ptr<QuantLib::BiCGstab>>(childNative.currentLink());
    
    _BiCGstabOwner = owner;
}
Cephei::QL::Math::Matrixutilities::CBiCGstab::CBiCGstab (QuantLib::Handle<QuantLib::BiCGstab> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phBiCGstab = &childNative;
	_ppBiCGstab = &static_cast<boost::shared_ptr<QuantLib::BiCGstab>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Math::Matrixutilities::CBiCGstab::CBiCGstab (QuantLib::BiCGstab childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phBiCGstab = NULL;
#endif
	_ppBiCGstab = new boost::shared_ptr<QuantLib::BiCGstab> (new QuantLib::BiCGstab (childNative));
    
}
#endif

Cephei::QL::Math::Matrixutilities::CBiCGstab::~CBiCGstab ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppBiCGstab != NULL)
    {
	    delete _ppBiCGstab;
        _ppBiCGstab = NULL;
    }
}
Cephei::QL::Math::Matrixutilities::CBiCGstab::!CBiCGstab ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppBiCGstab != NULL)
    {
	    delete _ppBiCGstab;
    }
}
QuantLib::BiCGstab& Cephei::QL::Math::Matrixutilities::CBiCGstab::GetReference ()
{
    if (_ppBiCGstab == NULL) throw REFNEW NativeNullException ();
	return **_ppBiCGstab;
}
boost::shared_ptr<QuantLib::BiCGstab>& Cephei::QL::Math::Matrixutilities::CBiCGstab::GetShared ()
{
    if (_ppBiCGstab == NULL) throw REFNEW NativeNullException ();
	return *_ppBiCGstab;
}
QuantLib::BiCGstab* Cephei::QL::Math::Matrixutilities::CBiCGstab::GetPointer ()
{
    if (_ppBiCGstab == NULL) throw REFNEW NativeNullException ();
	return &**_ppBiCGstab;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::BiCGstab>& Cephei::QL::Math::Matrixutilities::CBiCGstab::GetHandle ()
{
	if (_phBiCGstab == NULL)
	{
		_phBiCGstab = new Handle<QuantLib::BiCGstab> (*_ppBiCGstab);
	}
	return *_phBiCGstab;
}
#endif
bool Cephei::QL::Math::Matrixutilities::CBiCGstab::HasNative () 
{
	return (_ppBiCGstab != NULL);
}

Cephei::QL::Math::Matrixutilities::IBiCGStabResult^ Cephei::QL::Math::Matrixutilities::CBiCGstab::Solve (Cephei::QL::Math::IArray^ b, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Math::IArray^>^ x0)
{
    CArray^ _Cb;
    CArray^ _Cx0;
    try
    {
        _Cb = safe_cast<CArray^> (b);
        _Cb->Lock();
        QuantLib::Array& _b = static_cast<QuantLib::Array&> (_Cb->GetReference ()); 
        if (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Math::IArray^>::IsSome::get (x0))
        {
            _Cx0 = safe_cast<CArray^> (x0->Value);
            _Cx0->Lock();
        }
        QuantLib::Array& _x0 = 
            (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Math::IArray^>::IsSome::get (x0) ? static_cast<QuantLib::Array&> (_Cx0->GetReference ()) : QuantLib::Array()); //6
    	QuantLib::BiCGStabResult _rv = (QuantLib::BiCGStabResult)(*_ppBiCGstab)->solve ( _b,  _x0 );   
        Cephei::QL::Math::Matrixutilities::CBiCGStabResult^ _nrv = REFNEW Cephei::QL::Math::Matrixutilities::CBiCGStabResult (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cb != nullptr) _Cb->Unlock();
        if (_Cx0 != nullptr) _Cx0->Unlock();
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

